package edu.byu.intex2.data.dao;

import edu.byu.intex2.data.bo.PersonDAO; //What's this?
import edu.byu.intex2.data.Cache;
import edu.byu.intex2.data.ConnectionPool;
import edu.byu.intex2.data.DataException;
import java.sql.*;

import edu.byu.intex2.data.bo.*;
import edu.byu.intex2.data.*;

/**
 * person Data Access Object
 * @author Adam Chavez
 */
public class PersonDAO {

  /////////////////////////////////////////////
  ///   Singleton code

  private static PersonDAO instance = null;

  /** Creates a new instance of PersonDAO */
  private PersonDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized PersonDAO getInstance() {
    if (instance == null) {
      instance = new PersonDAO();
    }
    return instance;
  }//getInstance

  ////////////////////////////////////////////
  ///   CREATE methods

  /**
   * Creates a new person in the database
   * set alreadyInDB to false
   * put person in cache
   */
  public PersonDAO create(String id) throws DataException {

  }//create


  ////////////////////////////////////////////
  ///   READ methods

  /**
   * Reads an existing person from the database
   * 1) Checks cache
   * 2) Gets a connection
   * 3) Calls read with connection argument
   */
  public PersonDAO read(String id) throws DataException {
  }

  /**
   * Internal method to read an existing person from the database
   * 1) Check cache for object
   * 2) Create a prepared statement to execute sql query
   * 3) Create a resultset from the preparedstatement
   * 4) Return the person
   */
  synchronized PersonDAO read(String id, Connection conn) throws Exception {

  }//read


  /**
   * Internal method to create a person object from a record
   * 1) Check cache for object
   * 2) Instantiate new person
   * 3) Set alreadyInDb to false
   * 4) Set all variables from result set
   * 5) Set dirty to true
   * 6) Return person
   */
  synchronized PersonDAO readRecord(ResultSet rs, Connection conn) throws Exception {
  }//readRecord

  /////////////////////////////////////////////
  ///   UPDATE methods
  
  /** Saves an existing person in the database */
  public void save(person person) throws DataException {
    //get a connection from the pool, save all of the sales first using the other person method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
	
  }
  
  /** Internal method to update a person in the database */
  void save(person person, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the person object to cache
	//decide whether to update or insert into the database
	
    }  
  
  /** Saves an existing person to the database */
  private void update(person person, Connection conn) throws Exception {
    
	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement
	
  }
  
  /** Inserts a new person into the database */
  private void insert(person person, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
	
  }  
  
  
  
  
  
  /////////////////////////////////////////////////
  ///   DELETE methods
  
  /** We do not support deleting of business objects in this application */
  public void delete(person person) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }

  public Person setPerson(Person p){
      Connection conn = ConnectionPool.getInstance().get();
      try{
          PreparedStatement sql = conn.prepareStatement("SELECT * FROM person p WHERE p=?");
          sql.setString(1, p.getId());
      }
      catch(Exception e){
          throws new DataException("An error occurred while reading the business object information.", e);
      }
  }

}
